/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: cbus_dialer.c
 *
 * Purpose: implementation a dialer to connect to listener(service) and create communication pipe
 *
 * Developer:
 *   wen.gu , 2022-03-12
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#include "cbus/bus/cbus_dialer.h"
#include "cbus/bus/cbus_transport.h"

#include "cbus/core/cbus_log.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/
#define CHECK_OPS_API(ep_ops, ops_api) if(!(ep_ops)->ops_api){ \
    LOGE("syntax error: dialer ops must implementation API:'%s'\n", #ops_api ); \
    return GFALSE; \
}
/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/
struct _cbus_dialer
{
    GPHD dialer_handle;    
    cbus_pipe_ops* pipe_ops;
    cbus_dialer_ops dialer_ops;
};
/******************************************************************************
 **    inner FUNCTION DEFINITIONS
 ******************************************************************************/

static GBOL cbus_dialer_ops_check_valid(cbus_dialer_ops* ops)
{
    CHECK_OPS_API(ops, initialize);
    CHECK_OPS_API(ops, finish);
    CHECK_OPS_API(ops, connect);

    return GTRUE;
}
/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/

GErrc cbus_dialer_create(cbus_dialer_t* dialer, const GSTR trans_secheme_name)
{
    if (!dialer || !trans_secheme_name)
    {
        return G_ErrBadParameter;
    }

    cbus_transport_ops* trans_ops = cbus_transport_find(trans_secheme_name);

    if (!trans_ops)
    {
        return G_ErrNotFound;
    }

    if (cbus_dialer_ops_check_valid(trans_ops->dialer) == GFALSE)
    {
        return G_ErrUndefined;
    }


    cbus_dialer_t cd = (cbus_dialer_t)malloc(sizeof(struct _cbus_dialer));

    if (!cd)
    {
        return G_ErrInsufficientResources;
    }

    memset(cd, 0, sizeof(struct _cbus_dialer));

    /** Make a copy of the dialer operations.  This allows us to
	 *  modify them (to override NULLs for example), and avoids an extra
	 *  dereference on hot paths.
     */
    cd->dialer_ops = *trans_ops->dialer;
    cd->pipe_ops = trans_ops->pipe;

    GErrc ret = cd->dialer_ops.initialize(&cd->dialer_handle);

    if (G_OK != ret)
    {
        free(dialer);
        return ret;
    }

    *dialer = cd;
    
    return G_OK;
}

void cbus_dialer_destroy(cbus_dialer_t dialer)
{
    if (dialer)
    {
        GErrc ret = dialer->dialer_ops.finish(dialer->dialer_handle);

        if (G_OK != ret)
        {
            LOGE("finish dialer ops failed(0x%08x)\n", ret);
        }
        free(dialer);
    }
}

GErrc cbus_dialer_connect(cbus_dialer_t dialer, const GSTR url, GU32  timeout_ms, cbus_pipe_t* com_pipe)
{
    if (!dialer || !url || !com_pipe)
    {
        return G_ErrBadParameter;
    }

    GPHD pipe_handle = NULL;
    GErrc ret = dialer->dialer_ops.connect(dialer->dialer_handle, url, timeout_ms, &pipe_handle);

    if (G_OK == ret)
    {
        return cbus_pipe_create(com_pipe, pipe_handle, dialer->pipe_ops);
    }    
    
    return ret;
}